home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2b.lha / p4-1.2b / lib / p4_alloc.c < prev    next >
C/C++ Source or Header  |  1993-02-06  |  10KB  |  402 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. struct local_data *alloc_local_bm()
  5. {
  6.     struct local_data *l;
  7.  
  8.     l = (struct local_data *) p4_malloc(sizeof(struct local_data));
  9.     if (l == NULL)
  10.     {
  11.     p4_dprintf("OOPS: alloc_local_bm: p4_malloc failed \n");
  12.     return (l);
  13.     }
  14.  
  15.     l->am_bm = TRUE;
  16.     l->listener_fd = -1;
  17.     l->my_id = -1;
  18.     l->procgroup = NULL;
  19.     l->queued_messages = (struct p4_msg_queue *)
  20.     p4_malloc(sizeof(struct p4_msg_queue));
  21.     initialize_msg_queue(l->queued_messages);
  22.     l->soft_errors = 0;
  23.  
  24. #   ifdef CAN_DO_XDR
  25.     if ((l->xdr_buff = (char *) p4_malloc(XDR_BUFF_LEN)) == NULL)
  26.     {
  27.     p4_error("OOPS: alloc_local_bm: unable to malloc xdr_buff\n",NULL);
  28.     }
  29.     xdrmem_create(&(l->xdr_enc), l->xdr_buff, XDR_BUFF_LEN, XDR_ENCODE);
  30.     xdrmem_create(&(l->xdr_dec), l->xdr_buff, XDR_BUFF_LEN, XDR_DECODE);
  31. #   endif
  32.  
  33.     return (l);
  34. }
  35.  
  36. struct local_data *alloc_local_rm()
  37. {
  38.     struct local_data *l;
  39.  
  40.     l = (struct local_data *) p4_malloc(sizeof(struct local_data));
  41.     if (l == NULL)
  42.     {
  43.     p4_dprintf("OOPS: alloc_local_rm: p4_malloc failed \n");
  44.     return (l);
  45.     }
  46.  
  47.     l->am_bm = FALSE;
  48.     l->listener_fd = -1;
  49.     l->my_id = -1;
  50.     l->procgroup = NULL;
  51.     l->queued_messages = (struct p4_msg_queue *)
  52.     p4_malloc(sizeof(struct p4_msg_queue));
  53.     initialize_msg_queue(l->queued_messages);
  54.     l->soft_errors = 0;
  55.  
  56. #   ifdef CAN_DO_XDR
  57.     if ((l->xdr_buff = (char *) p4_malloc(XDR_BUFF_LEN)) == NULL)
  58.     {
  59.     p4_error("OOPS: alloc_local_rm: unable to malloc xdr_buff\n",NULL);
  60.     }
  61.     xdrmem_create(&(l->xdr_enc), l->xdr_buff, XDR_BUFF_LEN, XDR_ENCODE);
  62.     xdrmem_create(&(l->xdr_dec), l->xdr_buff, XDR_BUFF_LEN, XDR_DECODE);
  63. #   endif
  64.  
  65.     return (l);
  66. }                /* alloc_local_rm */
  67.  
  68. struct local_data *alloc_local_listener()
  69. {
  70.     struct local_data *l;
  71.  
  72.     l = (struct local_data *) p4_malloc(sizeof(struct local_data));
  73.  
  74.     l->am_bm = FALSE;
  75.     l->listener_fd = -1;
  76.     l->my_id = LISTENER_ID;
  77.     l->procgroup = NULL;
  78.     l->queued_messages = NULL;
  79.     l->xdr_buff = NULL;
  80.     l->soft_errors = 0;
  81.     return (l);
  82. }                /* alloc_local_listener */
  83.  
  84. struct local_data *alloc_local_slave()
  85. {
  86.     struct local_data *l;
  87.  
  88.     l = (struct local_data *) p4_malloc(sizeof(struct local_data));
  89.  
  90.     l->am_bm = FALSE;
  91.     l->listener_fd = -1;
  92.     l->my_id = -1;
  93.     l->procgroup = NULL;
  94.     l->queued_messages = (struct p4_msg_queue *)
  95.     p4_malloc(sizeof(struct p4_msg_queue));
  96.     initialize_msg_queue(l->queued_messages);
  97.     l->soft_errors = 0;
  98.  
  99. #   ifdef CAN_DO_XDR
  100.     if (!(p4_global->local_communication_only))
  101.     {
  102.         if ((l->xdr_buff = (char *) p4_malloc(XDR_BUFF_LEN)) == NULL)
  103.         {
  104.         p4_error("OOPS: alloc_local_slave: unable to malloc xdr_buff\n",NULL);
  105.         }
  106.         xdrmem_create(&(l->xdr_enc), l->xdr_buff, XDR_BUFF_LEN, XDR_ENCODE);
  107.         xdrmem_create(&(l->xdr_dec), l->xdr_buff, XDR_BUFF_LEN, XDR_DECODE);
  108.     }
  109. #   endif
  110.  
  111.     return (l);
  112. }
  113.  
  114. /*
  115.     This routine should be called before any sends and receives are done by
  116.     the user.  If not, some buffers may be lost.
  117. */
  118. P4VOID p4_set_avail_buff(bufidx,size)
  119. int bufidx;
  120. int size;
  121. {
  122.     p4_global->avail_buffs[bufidx].size = size;
  123.     p4_global->avail_buffs[bufidx].buff = NULL;
  124. }
  125.  
  126.  
  127. P4VOID init_avail_buffs()
  128. {
  129.     static int sizes[NUMAVAILS] = 
  130.             {64,256,1024,4096,16384,65536,262144,1048576};
  131.     int i;
  132.  
  133.     for (i = 0; i < NUMAVAILS; i++)
  134.     {
  135.         p4_global->avail_buffs[i].size = sizes[i];
  136.         p4_global->avail_buffs[i].buff = NULL;
  137.     }
  138. }
  139.  
  140. P4VOID p4_print_avail_buffs()
  141. {
  142.     int i, count;
  143.     struct p4_msg *next;
  144.  
  145.     p4_dprintf("avail lists for message buffers:\n");
  146.     p4_lock(&p4_global->avail_buffs_lock);
  147.     for (i = 0; i < NUMAVAILS; i++)
  148.     {
  149.     count = 0;
  150.     for (next = p4_global->avail_buffs[i].buff; next; next = next->link)
  151.          count++;
  152.     p4_dprintf("%d buffers of size %d\n",
  153.            count, p4_global->avail_buffs[i].size);
  154.     }
  155.     p4_unlock(&p4_global->avail_buffs_lock);
  156. }
  157.  
  158. struct p4_msg *alloc_p4_msg(msglen)
  159. int msglen;
  160. {
  161.     struct p4_msg *rmsg = NULL, **trailer;
  162.     int i, rounded, buff_len;
  163.     P4BOOL found;
  164.  
  165.     p4_dprintfl(40, "allocating a buffer for message of size %d\n", msglen);
  166.  
  167. #   if defined(TCMP)
  168.  
  169.     buff_len = sizeof(struct p4_msg) + msglen - sizeof(char *);
  170.     rmsg = (struct p4_msg *) tcmp_allocate(buff_len);
  171.     p4_dprintfl(40, "allocated new buffer at 0x%x for msg of size %d\n",
  172.         rmsg, msglen);
  173.     rmsg->len = msglen;
  174.     rmsg->orig_len = msglen;
  175.     return(rmsg);
  176.  
  177. #   else
  178.  
  179.     i = 0;
  180.     while ((i < NUMAVAILS) && (msglen > p4_global->avail_buffs[i].size))
  181.     i++;
  182.  
  183.     if (i == NUMAVAILS)        /* didn't find a big enough avail buffer */
  184.     {
  185.         buff_len = sizeof(struct p4_msg) + msglen - sizeof(char *);
  186.     rmsg = (struct p4_msg *) p4_shmalloc(buff_len);
  187.     p4_dprintfl(40, "allocated new buffer at0x%x for message size %d\n",
  188.             rmsg, msglen);
  189.     }
  190.     else
  191.     {
  192.     rounded = p4_global->avail_buffs[i].size;
  193.     buff_len = sizeof(struct p4_msg) + rounded - sizeof(char *);
  194.     p4_lock(&p4_global->avail_buffs_lock);
  195.     if (p4_global->avail_buffs[i].buff)
  196.     {
  197.  
  198. #if defined(IPSC860)
  199.         rmsg = p4_global->avail_buffs[i].buff;
  200.         trailer = &(p4_global->avail_buffs[i].buff);
  201.         found = FALSE;
  202.         while (!found && (rmsg != NULL))
  203.         {
  204.         if (rmsg->msg_id == -1)
  205.         {
  206.             found = TRUE;
  207.         }
  208.         else if (msgdone((long) rmsg->msg_id))
  209.         {
  210.             rmsg->msg_id = -1;    
  211.             (p4_global->cube_msgs_out)--;
  212.             found = TRUE;
  213.         }
  214.         else
  215.         {
  216.             trailer = &((*trailer)->link);
  217.             rmsg = rmsg->link;
  218.         }
  219.         }
  220.         if (!found && (p4_global->cube_msgs_out > P4_MAX_CUBE_MSGS_OUT))
  221.         {
  222.         if ((rmsg = p4_global->avail_buffs[i].buff) != NULL)
  223.         {
  224.             msgwait((long) rmsg->msg_id);
  225.             rmsg->msg_id = -1;    
  226.             (p4_global->cube_msgs_out)--;
  227.             found = TRUE;
  228.         }
  229.         }
  230.         if (!found)
  231.         {
  232.         rmsg = (struct p4_msg *) p4_shmalloc(buff_len);
  233.         p4_dprintfl(40, "allocated new buffer at 0x%x of size %d for message size %d\n", rmsg, rounded, msglen);
  234.         }
  235.         else
  236.         {
  237.         *trailer = rmsg->link;
  238.         p4_dprintfl(40, "reused a buffer of size %d for message size %d\n",
  239.                 rounded, msglen);
  240.         }
  241. #else
  242.         rmsg = p4_global->avail_buffs[i].buff;
  243.         p4_global->avail_buffs[i].buff = rmsg->link;
  244.         p4_dprintfl(40, "reused a buffer of size %d for message size %d\n",
  245.             rounded, msglen);
  246. #endif
  247.  
  248.         p4_unlock(&p4_global->avail_buffs_lock);
  249.     }
  250.     else
  251.     {
  252.         p4_unlock(&p4_global->avail_buffs_lock);
  253.         rmsg = (struct p4_msg *) p4_shmalloc(buff_len);
  254.         p4_dprintfl(40, "allocated new buffer at 0x%x of size %d for message size %d\n", rmsg, rounded, msglen);
  255.     }
  256.     }
  257.  
  258.     if ((rmsg == NULL) && !SOFTERR)
  259.     p4_error("alloc_p4_msg failed", 0);
  260.  
  261.     rmsg->len = msglen;
  262.     rmsg->orig_len = msglen;
  263.     return(rmsg);
  264.  
  265. #   endif
  266. }                /* alloc_p4_msg */
  267.  
  268. P4VOID free_p4_msg(tmsg)
  269. struct p4_msg *tmsg;
  270. {
  271.     int i;
  272.     struct p4_msg *p;
  273.  
  274.     p4_dprintfl(40, "freeing a buffer with bufflen=%d msglen=%d\n", 
  275.         tmsg->orig_len,tmsg->len);
  276.  
  277.     /* Sanity check here as bad message pointer causes havoc */
  278.  
  279.     if ((tmsg->orig_len < 0) || (tmsg->orig_len > P4_MAX_MSGLEN))
  280.     p4_error("free_p4_msg: bad hdr: msglen out of range", tmsg->len);
  281.  
  282. #   if defined(TCMP)
  283.     if (tmsg)
  284.     tcmp_deallocate(tmsg);
  285.     return;
  286.  
  287. #   else
  288.  
  289.     i = 0;
  290.     while ((i < NUMAVAILS) && (tmsg->orig_len > p4_global->avail_buffs[i].size))
  291.     i++;
  292.  
  293.     if (i == NUMAVAILS)
  294.     {
  295.     /* buffer being freed is not a kept size */
  296.     p4_shfree(tmsg);
  297.     p4_dprintfl(40, "freeing a buffer at %d with bufflen=%d msglen=%d\n", 
  298.             tmsg,tmsg->orig_len,tmsg->len);
  299.     }
  300.     else
  301.     {
  302.     /* hook new buffer in at end of list */
  303.     p4_lock(&p4_global->avail_buffs_lock);
  304.     if ((p = p4_global->avail_buffs[i].buff) == NULL)
  305.         p4_global->avail_buffs[i].buff = tmsg;
  306.     else
  307.     {
  308.         while (p->link != NULL)
  309.         p = p->link;
  310.         p->link = tmsg;
  311.     }
  312.     tmsg->link = NULL;
  313.     p4_unlock(&p4_global->avail_buffs_lock);
  314.     p4_dprintfl(40, "saved a buffer of size %d in avail list for size %d\n",
  315.             tmsg->orig_len, p4_global->avail_buffs[i].size);
  316.     }
  317.  
  318. #   endif
  319. }                /* free_p4_msg */
  320.  
  321. P4VOID alloc_global()
  322. {
  323.     int i;
  324.     struct p4_global_data *g;
  325.  
  326.     p4_global = (struct p4_global_data *) p4_shmalloc(sizeof(struct p4_global_data));
  327.     if (p4_global == NULL)
  328.     {
  329.     p4_error("alloc_global: alloc_global failed\n", sizeof(struct p4_global_data));
  330.     }
  331.     g = p4_global;
  332.  
  333. #ifdef SYSV_IPC
  334.     g->slave_lock.semid = sysv_semid0;
  335.     g->slave_lock.semnum = 1;
  336.     g->sysv_semid[0] = sysv_semid0;
  337.     g->sysv_num_semids = 1;
  338.     g->sysv_next_lock = 2;  /* shmem_lock is 0 & slave_lock is 1 */
  339. #else
  340.     p4_lock_init(&g->slave_lock);
  341. #endif
  342.  
  343.     g->listener_pid = -1;
  344.     g->listener_port = -1;
  345.  
  346.     g->cube_msgs_out = 0;
  347.  
  348.     g->local_slave_count = 0;
  349.     g->local_communication_only = TRUE;
  350.     g->n_forked_pids = 0;
  351.  
  352.     for (i = 0; i < P4_MAX_MSG_QUEUES; i++)
  353.     {
  354.     initialize_msg_queue(&g->shmem_msg_queues[i]);
  355.     g->dest_id[i] = -1;
  356.     }
  357.  
  358.     p4_lock_init(&g->avail_buffs_lock);
  359.     init_avail_buffs();
  360.     p4_lock_init(&g->avail_quel_lock);
  361.     g->avail_quel = NULL;
  362.  
  363.     g->num_in_proctable = 0;
  364.     g->num_installed = 0;
  365.     g->my_host_name[0] = '\0';
  366.     get_qualified_hostname(g->my_host_name);
  367.  
  368.     p4_barrier_init(&(g->cluster_barrier));
  369.  
  370.     sprintf(g->application_id, "p4_%-8d", getpid());
  371.  
  372. #   if defined(P4BSD)
  373.     g->max_connections = getdtablesize();
  374. #   endif
  375.  
  376. #   if defined(P4SYSV)
  377. #       if defined(CRAY) || defined(RS6000) || \
  378.        defined(IBM3090) || defined(SYMMETRY_PTX)
  379.               g->max_connections = getdtablesize();
  380. #       else
  381. #       if defined(IPSC860) || defined(HP)  ||  defined(NCUBE)
  382.               g->max_connections = 20;    /* HP probably supports more with sysconf */
  383. #       else
  384.               g->max_connections = (int) ulimit(4,0);
  385. #       endif
  386. #       endif
  387. #   endif
  388.  
  389. }
  390.  
  391. struct listener_data *alloc_listener_info()
  392. {
  393.     struct listener_data *l;
  394.  
  395.     l = (struct listener_data *) p4_malloc(sizeof(struct listener_data));
  396.  
  397.     l->listening_fd = -1;
  398.     l->slave_fd = -1;
  399.  
  400.     return (l);
  401. }
  402.